package com.dg11185.nearshop.baidumap;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;

import com.android.nearshop.R;
import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMyLocationClickListener;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.dg11185.nearshop.app.ViewUtils;
import com.dg11185.nearshop.baidumap.MyMapLocation.MyBDLocationListener;
import com.dg11185.nearshop.baidumap.ZoomLayout.ZoomClickListener;
import com.umeng.analytics.MobclickAgent;

/**
 * 地图相关界面的公共类
 * 
 * @author：Daibo
 * @time：2014-7-10 下午2:40:35
 */
public abstract class BaseMapActivity extends Activity implements MyBDLocationListener,ZoomClickListener {
    protected MapView mMapView = null;

    protected BaiduMap mBaiduMap = null;

    /**自定义百度地图定位适配器，用于获取定位数据*/
    protected MyMapLocation mapLocation = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView();
        initMapView();
    }

    /**
     * 
     */
    public abstract void setContentView();

    /**
     * 初始化MapView的方法，它比其他任何方法都先被Activity调用；
     * 
     * @Exception NullPointerException
     */
    private void initMapView() {

        BaiduMapOptions mapOptions = new BaiduMapOptions().zoomControlsEnabled(false);
        LinearLayout mapLayout = (LinearLayout) this.findViewById(R.id.shop_base_mapview_layout);
        mMapView = new MapView(this, mapOptions);
        mapLayout.addView(mMapView);
        mBaiduMap = mMapView.getMap();
        MapStatusUpdate update = MapStatusUpdateFactory.newLatLngZoom(MapConstant.getDefaultPoint(), 16.0f);
        mBaiduMap.setMapStatus(update);// 调用{@link BaiduMap
                                       // #animateMapStatus}来切换地图位置会导致系统卡死

        ZoomLayout zoomLayout = (ZoomLayout) this.findViewById(R.id.map_base_zoom_layout);
        zoomLayout.setMapView(mMapView);
        zoomLayout.setListener(this);
    };

    @Override
    protected void onResume() {
        mMapView.onResume();
        super.onResume();
        MobclickAgent.onPageStart(getClass().getName());
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getName());
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        mMapView.onDestroy();
        mMapView = null;
        super.onDestroy();
    }

    /**
     * 添加定位图层,在{@link #onReceiveLocation(BDLocation)中处理回掉事件}
     */
    public void addLocOverlay() {
        ViewUtils.showToast(BaseMapActivity.this, "定位中...");
        mBaiduMap.setMyLocationEnabled(true);
        mBaiduMap.setOnMyLocationClickListener(locationListener);
        mapLocation = new MyMapLocation(this, this);
        mapLocation.start();
    }

    @Override
    /**
     * 百度地图定位事件回掉，需要先调用{@link #addLocOverlay()}方法；
     */
    public void onReceiveLocation(BDLocation location) {
        // map view 销毁后不在处理新接收的位置
        if (location == null || mMapView == null)
            return;
        MyLocationData locData = new MyLocationData.Builder().accuracy(location.getRadius())
                .direction(location.getDirection()).latitude(location.getLatitude()).longitude(location.getLongitude())
                .build();
        mBaiduMap.setMyLocationData(locData);

        LatLng locPoi = new LatLng(location.getLatitude(), location.getLongitude());
        MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(locPoi);
        mBaiduMap.animateMapStatus(update);
    }

    /**
     * 定位图层的点击事件
     */
    private OnMyLocationClickListener locationListener = new OnMyLocationClickListener() {

        @Override
        public boolean onMyLocationClick() {
            // TODO
            return false;
        }
    };

    /**
     * 放大缩小按钮点击事件回调
     */
    public void onZoomClicked(int zoomLevel) {
        MapStatusUpdate update = MapStatusUpdateFactory.zoomTo(zoomLevel);
        mBaiduMap.animateMapStatus(update);
    };
    
    /**
     * 重定位按钮点击事件回调
     */
    @Override
    public void onRelocationsClicked() {
        if (mapLocation == null) {
            addLocOverlay();
        } else {
            mapLocation.reStart();
        }
    }
}
